var postData = require("../../../data/posts-data.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    // var globalData = app.globalData;
    var postId = option.id;
    this.data.currentPostId = postId;
    var postObj = postData.postList[postId];
    this.setData({ postObj: postObj });

    //收藏开始
    //初始化代码
    var postsCollected = wx.getStorageSync("posts_collected");
    if (postsCollected){
      var postCollected = postsCollected[postId];
      this.setData({
        collected:postCollected
      });
    }else{
      var postsCollected={};
      postsCollected[postId]=false;
      wx.setStorageSync("posts_collected", postsCollected);
    }
    //全局g_isPlayingMusic判断
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === this.data.currentPostId){
     // this.data.isPlayingMusic = true;
     this.setData({
       isPlayingMusic : true
     });
    }

    var that = this;
    wx.onBackgroundAudioPlay(function(){
      that.setData({
        isPlayingMusic : true
      });
      app.globalData.g_isPlayingMusic = true;
      app.globalData.g_currentMusicPostId = that.data.currentPostId;
    });

    wx.onBackgroundAudioPause(function () {
      that.setData({
        isPlayingMusic: false
      });
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });
    //音乐播放停止监听事件
    wx.onBackgroundAudioStop(function () {
      that.setData({
        isPlayingMusic: false
      });
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    });
  },
  //点击收藏/取消收藏
  onCollectionTap:function(event){
    //获取缓存的值
    var postsCollected = wx.getStorageSync("posts_collected");
    //判断当前对象是否收藏
    var postCollected = postsCollected[this.data.currentPostId];
    //取反操作
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    wx.setStorageSync("posts_collected", postsCollected);
    //更新数据，切换图片
    this.setData({
      collected:postCollected
    });
    wx.showToast({
      title: postCollected ? '收藏成功':'取消成功',
      duration: 1000,
      icon: 'none'
    });

  },
  onShareTap:function(event){
    var itemList = ['分享给微信好友', '分享到盆友圈', '分享到QQ', '分享到微博'];
    wx.showActionSheet({
      itemList: itemList,
      itemColor:"#405f80",
      success: function (res) {
        //res.tapIndex
        wx.showModal({
          title: '用户 ' + itemList[res.tapIndex],
          content: '用户是否取消？'+res.cancel+'现在还无法实现分享, 什么时候支持呢?',
        })
      },
      fail: function (res) {
        console.log(res.errMsg);
      }
    })
  },
  onMusicTap: function (event){
    var currentPostId = this.data.currentPostId;
    console.log(currentPostId);
    var postDataObj = postData.postList[currentPostId];
    var isPlayingMusic = this.data.isPlayingMusic;
    if (isPlayingMusic){
      wx.pauseBackgroundAudio();//暂停
      this.setData({
        isPlayingMusic:false
      });
    }else{
      wx.playBackgroundAudio({
        dataUrl: postDataObj.music.dataUrl,
        title: postDataObj.music.title,
        coverImgUrl: postDataObj.music.coverImgUrl
      });
      this.setData({
        isPlayingMusic: true
      });
    }
    
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})